package com.xiuyou.tracksdk.tracksdk.utils;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Environment;
import android.util.Log;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
 * Created by lhy on 2016/3/29.
 */
public class CacheManager {
    private static CacheManager INSTANCE = null;
    private CacheManager(){

    }
    public static CacheManager getInstance(){
        if(INSTANCE == null){
            synchronized (CacheManager.class){
                if(INSTANCE == null){
                    INSTANCE = new CacheManager();
                }
            }
        }
        return INSTANCE;
    }

    /**
     * 获取包名
     * @param context
     * @return
     */
    public static String getApplicationPakageName(Context context) {
        try {
            PackageInfo pkg = context.getPackageManager().getPackageInfo(
                    context.getApplicationContext().getPackageName(), 0);
            String pkgName = pkg.packageName;
            return pkgName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取手机内存中的data/data/com.……
     * @param context
     * @return
     */
    public static String getApplicationDataDirectory(Context context) {
        String path = Environment.getDataDirectory().getAbsolutePath();
        return path + "/data/" + getApplicationPakageName(context);
    }

    /**
     * 文件缓存写入
     * @param datas
     * @param context
     * @param uri
     */
    public static void writeObject(Object datas, Context context, String uri) {
        File dir = new File(getApplicationDataDirectory(context) + "/cache");
        if (!dir.exists()) {
            dir.mkdirs();
        }
        File file = new File(getApplicationDataDirectory(context) + "/cache/"+uri+".txt");
        if(file.exists()){
            file.delete();
        }
        try {
            FileOutputStream outStream = new FileOutputStream(
                    getApplicationDataDirectory(context) + "/cache/"+uri+".txt");
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(
                    outStream);
            objectOutputStream.writeObject(datas);
            outStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 文件缓存读取
     * @param context
     * @param uri
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> T readObject(Context context, String uri, Class<T> clazz) {
        FileInputStream freader;
        T datas = null;
        try {
            datas = clazz.newInstance();
            freader = new FileInputStream(
                    getApplicationDataDirectory(context) + "/cache/"+uri+".txt");
            ObjectInputStream objectInputStream = new ObjectInputStream(freader);
            datas = (T) objectInputStream.readObject();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return datas;
    }

}
